package org.eiahe.hr.interview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.satoken.utils.LoginHelper;
import org.eiahe.hr.interview.domain.InterviewPositionCategory;
import org.eiahe.hr.interview.domain.dto.InterviewPositionCategoryQueryDTO;
import org.eiahe.hr.interview.domain.vo.InterviewPositionCategoryVO;
import org.eiahe.hr.interview.domain.dto.InterviewPositionCategoryDTO;
import org.eiahe.hr.interview.mapper.InterviewPositionCategoryMapper;
import org.eiahe.hr.interview.service.InterviewPositionCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 职位分类表
 * @author :zhangnn
 * @className :InterviewPositionCategoryServiceImpl
 * @description: TODO
 * @date 2025-08-13 16:02:49
 */
@Slf4j
@Service
public class InterviewPositionCategoryServiceImpl
    extends ServiceImpl<InterviewPositionCategoryMapper, InterviewPositionCategory>
    implements InterviewPositionCategoryService {

    /**
     * 分页
     * @param queryDTO
     * @return
     */
    @Override
    public TableDataInfo<InterviewPositionCategoryVO> selectCategoryPage(InterviewPositionCategoryQueryDTO queryDTO, PageQuery pageQuery) {
        LambdaQueryWrapper<InterviewPositionCategory> categoryLambdaQueryWrapper = Wrappers.lambdaQuery();
        categoryLambdaQueryWrapper.like(StringUtils.isNotBlank(queryDTO.getCategoryName()),InterviewPositionCategory::getCategoryName,queryDTO.getCategoryName());
        categoryLambdaQueryWrapper.like(StringUtils.isNotBlank(queryDTO.getCategoryCode()),InterviewPositionCategory::getCategoryCode,queryDTO.getCategoryCode());
        categoryLambdaQueryWrapper.eq(null != queryDTO.getStatus(),InterviewPositionCategory::getStatus,queryDTO.getStatus());
        Page<InterviewPositionCategoryVO> page = this.baseMapper.selectVoPage(pageQuery.build(),categoryLambdaQueryWrapper);
        return TableDataInfo.build(page);
    }


    /**
     * 查询所有可见的 分类
     * @return
     */
    @Override
    public List<InterviewPositionCategoryDTO> selectAllEnabled() {
        LambdaQueryWrapper<InterviewPositionCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InterviewPositionCategory::getStatus, 1)
            .orderByAsc(InterviewPositionCategory::getCategoryCode);

        return this.list(wrapper).stream()
            .map(this::convertToDTO)
            .collect(Collectors.toList());
    }

    @Override
    public InterviewPositionCategoryDTO selectById(Long id) {
        InterviewPositionCategory entity = this.getById(id);
        return entity != null ? convertToDTO(entity) : null;
    }

    /**
     * 新增
     * @param categoryDTO
     * @return
     */
    @Override
    public boolean insertCategory(InterviewPositionCategoryDTO categoryDTO) {
        InterviewPositionCategory entity = convertToEntity(categoryDTO);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setCreateBy(LoginHelper.getUsername());
        return this.save(entity);
    }

    /**
     * 修改职位分类
     * @param categoryDTO
     * @return
     */
    @Override
    public boolean updateCategory(InterviewPositionCategoryDTO categoryDTO) {
        InterviewPositionCategory entity = convertToEntity(categoryDTO);
        entity.setUpdateTime(new Date());
        entity.setUpdateBy(LoginHelper.getUsername());
        return this.updateById(entity);
    }

    /**
     * 删除职位分类
     * @param id
     * @return
     */
    @Override
    public boolean deleteCategory(Long id) {
        return this.removeById(id);
    }


    /**
     * 批量删除职位分类
     * @param ids
     * @return
     */
    @Override
    public boolean deleteCategoryByIds(List<Long> ids) {
        return this.removeByIds(ids);
    }


    @Override
    public boolean checkCategoryCodeExists(String categoryCode, Long excludeId) {
        LambdaQueryWrapper<InterviewPositionCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InterviewPositionCategory::getCategoryCode, categoryCode);
        if (excludeId != null) {
            wrapper.ne(InterviewPositionCategory::getId, excludeId);
        }
        return this.count(wrapper) > 0;
    }



    /**
     * 实体转DTO
     */
    private InterviewPositionCategoryDTO convertToDTO(InterviewPositionCategory entity) {
        InterviewPositionCategoryDTO dto = new InterviewPositionCategoryDTO();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    /**
     * DTO转实体
     */
    private InterviewPositionCategory convertToEntity(InterviewPositionCategoryDTO dto) {
        InterviewPositionCategory entity = new InterviewPositionCategory();
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }
}
